<?php
/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 陈风任 <491085389@qq.com>
 * Date: 2019-1-25
 */

namespace app\user\controller;

use think\Page;
use think\Db;
use think\Config;
use think\Cookie;
use think\Request;

class Coupon extends Base
{
    public function _initialize()
    {
        parent::_initialize();
    }

    // 我的优惠券
    public function index()
    {
        $users = $this->users;
        $use_status = input('param.use_status',0);
        //判断是否有过期优惠券
        Db::name('shop_coupon_use')->where(['users_id' => $users['users_id'], 'use_status' => 0])->where('end_time', '<', getTime())->update(['use_status' => 2, 'update_time' => getTime()]);

        $where['a.users_id'] = $users['users_id'];
        $where['a.use_status'] = $use_status;

        $count = Db::name('shop_coupon_use')->alias('a')->where($where)->count('use_id');
//        $Page = $pager = new Page($count, config('paginate.list_rows'));
        $Page = $pager = new Page($count, 12);

        $list = Db::name('shop_coupon_use')
            ->alias('a')
            ->field('b.*,a.*')
            ->join('shop_coupon b','a.coupon_id = b.coupon_id','left')
            ->where($where)
            ->order('end_time asc')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
        $eyou['list'] = $list;
        $eyou['count'] = Db::name('shop_coupon_use')->where('users_id',$users['users_id'])->group('use_status')->field('count(use_id) as count,use_status')->getAllWithIndex('use_status');
        $show = $Page->show();
        $this->assign('page',$show);
        $this->assign('pager',$pager);
        $this->assign('eyou', $eyou);

        return $this->fetch('users/coupon_index');
    }
}